package demo

import (
	"log"
	"net"
)

// cmd/client/main.go
func main() {
	conn, err := net.Dial("tcp", "localhost:8080")
	if err != nil {
		log.Fatal(err)
	}

	codec := &codec.LengthHeaderCodec{
		TypeResolver: func(id uint32) proto.Message {
			return nil // 客户端不需要解析
		},
	}

	tcpConn := conn.NewTCPConn(conn, codec, nil)

	// 发送登录请求
	req := &proto.LoginReq{
		Username: "admin",
		Password: "123456",
	}
	if err := tcpConn.Send(1001, req); err != nil {
		log.Fatal(err)
	}

	// 接收响应
	for {
		protocolID, msg, err := codec.Decode(tcpConn.reader)
		if err != nil {
			log.Fatal(err)
		}

		switch protocolID {
		case 1002:
			if resp, ok := msg.(*proto.LoginResp); ok {
				log.Printf("登录响应: %+v", resp)
				return
			}
		}
	}
}
